<?php
class poll extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('poll_model','poll');
		$this->load->model('polldetail_model','polldetail');
		$this->load->model('pollresult_model','pollresult');
	}
	
	function index(){
		$data['poll'] = $this->poll->where('active = 1')->get_row();
		$data['polldetails'] = $this->polldetail->where('poll_id = '.$data['poll']['id'])->order_by('id','asc')->get();
		$this->template->build('index',$data);
	}
	
	function view($id = FALSE)
	{
		$data['polls'] = $this->poll->get("select polldetails.*,round((count(pollresults.poll_id)/(select count(poll_id) from pollresults where poll_id = $id)*100),2) percent,count(pollresults.id) num
from polldetails left join pollresults
on polldetails.id = pollresults.polldetail_id
where polldetails.poll_id = $id
group by pollresults.polldetail_id
order by id asc");
		$this->load->view('poll_view',$data);
	}
	
	function vote()
	{
		if($_POST){
			$_POST['created'] = date("Y-m-d H:i:s");
			$_POST['ip'] = $_SERVER["REMOTE_ADDR"];
			$this->pollresult->save($_POST);
			
			set_notify('success', 'บันทึกข้อมูลเรียบร้อยแล้ว');
		}
		redirect('home');
	}
}
?>